package com.zzyl.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.BedDto;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.service.BedService;
import com.zzyl.vo.BedVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/bed")
@Api(tags = "床位管理相关接口")
public class BedController extends BaseController {

    @Autowired
    private BedService bedService;

    @GetMapping("/read/room/{roomId}")
    @ApiOperation(value = "根据房间id查询床位", notes = "传入房间id")
    public ResponseResult<List<BedVo>> readBedByRoomId(
            @ApiParam(value = "房间ID", required = true) @PathVariable("roomId") Long roomId) {
        List<BedVo> beds = bedService.getBedsByRoomId(roomId);
        return success(beds);
    }

    /**
     * 处理床位新增请求
     *
     * @param bedDto
     * @return
     */
    @PostMapping("/create")
    @ApiOperation("创建床位")
    @ApiImplicitParam(name = "bedDto", value = "床位dto实体类对象", required = true, dataType = "BedDto")
    @ApiOperationSupport(includeParameters = {"bedDto.bedNumber", "bedDto.sort", "bedDto.roomId"})
    public ResponseResult createBed(@RequestBody BedDto bedDto) {
        log.info("处理床位新增请求，请求参数：{}", bedDto);
        try {
            bedService.create(bedDto);
            return success();
        } catch (Exception e) {
            throw new BaseException(BasicEnum.BED_INSERT_FAIL);
        }
    }

    /**
     * 处理根据ID查询床位请求
     *
     * @param id
     * @return
     */
    @GetMapping("/read/{id}")
    @ApiOperation(value = "根据id查询床位", notes = "传入床位id")
    public ResponseResult readBed(@ApiParam(value = "床位ID", required = true) @PathVariable("id") Long id) {
        BedVo bed = bedService.getBedById(id);
        return success(bed);
    }

    /**
     * 处理床位更新请求
     *
     * @param bedDto
     * @return
     */
    @PutMapping("/update")
    @ApiOperation(value = "更新床位", notes = "传入床位对象，包括床位id、床位号、所属房间号等信息")
    public ResponseResult updateBed(@RequestBody BedDto bedDto) {
        log.info("处理床位更新请求,请求参数{}", bedDto);
        bedService.update(bedDto);
        return success();
    }

    /**
     * 处理床位删除请求
     *
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除床位",notes = "传入床位id")
    public ResponseResult deleteBed (@ApiParam(value = "床位ID",required = true) @PathVariable("id") Long id) {
        bedService.deleteBedById(id);
        return success();
    }
}
